﻿@using KPIS.GERP.Models.ICM
@*@using KPIS.GERP.WEB.GIMS.Classes*@
@model PurchaseOrder

@Html.HiddenFor(model => model.PoId)
@Html.HiddenFor(model => model.RecordStatus) 

@if(Model != null)
{

<div class="widget-box">
	<div class="widget-header header-color-blue2">
        <div class="widget-toolbar">
            <a href="#" data-action="collapse" style="color:#fff;font-size:large;" ><i class="icon-chevron-up"></i>  ข้อมูลหลัก</a>
		</div>
    </div>
    <div class="widget-body">
        <div class="widget-body-inner">
			<div class="widget-main form-horizontal">
                <div class="span12">
                <div class="control-group">
				    <div class="control-label" style="font-size:x-large;font-weight:bold; margin-right:25px;">
                        @(Model.IsContract ?? false ? "สัญญา" : "ใบสั่ง")
                    </div>
                    <div>
                        @if(Model.IsContract == true){
                            <button type="button" class="btn btn-gray button-standard-width btn-small" onclick="window.location.href ='@Url.Action("ReportPoCotact", new { PoId = Model.PoId })'" role="button" >
                                รายงานสัญญาจ้าง
                            </button>
                        }else
                        {
                            if (Model.MasPurchaseType.PcTypeName == "ซื้อ")
                            {
                            <button type="button" class="btn btn-gray button-standard-width btn-small" onclick="window.location.href ='@Url.Action("ReportPoPurchase", new { PoId = Model.PoId })'" role="button" >
                                รายงานใบสั่งซื้อ
                            </button>
                            }
                            else
                            {
                             <button type="button" class="btn btn-gray button-standard-width btn-small" onclick="window.location.href ='@Url.Action("ReportPoPurchaseContact", new { PoId = Model.PoId })'" role="button" >
                                รายงานใบสั่งจ้าง
                            </button>
                            }
                        }
                    </div>
                    @*<div class="controls-detail" style="margin-top: 5px !important;font-size:x-large;font-weight:bold;">
						    
                    </div>*@
                </div>
                <div class="control-group">
					<div class="control-label">
						@Html.DisplayNameFor(model => model.BgYear)
					</div>
					<div class="controls-detail" style="margin-top: 5px !important;">
						@Html.DisplayFor(model => model.BgYear)
					</div>
                </div>
                <div class="control-group">
				    <div class="control-label">
						@Html.DisplayNameFor(model => model.MasPurchaseType.PcTypeName)
                    </div>
                    <div class="controls-detail" style="margin-top: 5px !important;">
						@Html.DisplayFor(model => model.MasPurchaseType.PcTypeName)
                    </div>
                </div>
                <div class="control-group">
					<div class="control-label">
						@Html.DisplayNameFor(model => model.Code)
					</div>
					<div class="controls-detail" style="margin-top: 5px !important;font-weight: bold;">
						@Html.DisplayFor(model => model.Code)
					</div>
                </div>
                <div class="control-group">
                    <div class="control-label">
                        @Html.DisplayNameFor(model => model.Name)
                    </div>
                    <div class="controls-detail" style="margin-top: 5px !important; font-weight: bold;">
                        @Html.DisplayFor(model => model.Name)
                    </div>
                </div>
                <div class="control-group">
					<div class="control-label">
						@*@Html.DisplayNameFor(model => model.Bidder.PurchaseRequest.Code)*@
                        เลขที่อ้างอิงขอซื้อขอจ้าง
					</div>
                    <div class="controls-detail" style="margin-top: 5px !important;">
                        @if (Model != null && Model.Bidder != null && Model.Bidder.PurchaseRequest != null)
                        {
                            <a href="@Url.Action("Details", "Purchase", new{Id=Model.Bidder.PurchaseRequest.PrId})" title="คลิกเพื่อย้อนกลับไปดูใบซื้อจ้าง" target="_blank">
                                <u>@Model.Bidder.PurchaseRequest.Code - 
                                @Model.Bidder.PurchaseRequest.Name &nbsp; &nbsp;<i class="icon-external-link"></i></u>
                            </a>
                        }
                    </div>
                </div>
                <div class="control-group">
                    <div class="control-label">
						@Html.DisplayNameFor(model => model.Bidder.MasContact.NameTh)
                    </div>
					<div class="controls-detail" style="margin-top: 5px !important;">
						@Html.DisplayFor(model => model.Bidder.MasContact.NameTh)
                    </div>
                </div>
                <div class="control-group">
                    <div class="control-label">
						@Html.DisplayNameFor(model => model.Descr)
                    </div>
					<div class="controls-detail" style="margin-top: 5px !important;">
						@Html.DisplayFor(model => model.Descr)
                    </div>
                </div>
                <div class="control-group">
				    <div class="control-label">
                        @Html.DisplayNameFor(model => model.OrderDate)
                    </div>
                    <div class="controls-detail" style="margin-top: 5px !important;">
						@Html.DisplayFor(model => model.OrderDate, "LongDate")
                    </div>
                </div> 
                <div class="control-group">
                    <div class="control-label">
						@Html.DisplayNameFor(model => model.DeliveryDate)
                    </div>
					<div class="controls-detail" style="margin-top: 5px !important;">
						@Html.DisplayFor(model => model.DeliveryDate, "LongDate")
                    </div>
                </div>                           
                
                <div class="control-group">
                    <div class="control-label">
                        @Html.DisplayNameFor(model => model.CanInstallment)
                    </div>
					<div class="controls-detail" style="margin-top: 5px !important;">
                        @if (Model.CanInstallment.Equals("Y")){
                            @Html.Display("ส่งมอบหลายงวด")
                        }else{
                            @Html.Display("ส่งมอบงวดเดียว")
                        }
                    </div>
                </div>
                
                @if (Model.IsContract ?? false)
                {
                <div class="control-group">
                    <div class="control-label">
						@Html.DisplayNameFor(model => model.DateReturnContract)
                    </div>
				    <div class="controls-detail" style="margin-top: 5px !important;">
						@Html.DisplayFor(model => model.DateReturnContract)
                    </div>
                </div>
                <div class="control-group">
                    <div class="control-label">
						@Html.DisplayNameFor(model => model.DateReturnPrepay)
                    </div>
					<div class="controls-detail" style="margin-top: 5px !important;">
						@Html.DisplayFor(model => model.DateReturnPrepay)
                    </div>
                </div>
                 <div class="control-group">
                    <div class="control-label">
						@Html.DisplayNameFor(model => model.DateReturnRedemption)
                    </div>
					<div class="controls-detail" style="margin-top: 5px !important;">
						@Html.DisplayFor(model => model.DateReturnRedemption)
                    </div>
                </div>
                <div class="control-group">
                    <div class="control-label">                       
						@Html.DisplayNameFor(model => model.PrepayAmount)
                    </div>
					<div class="controls-detail" style="margin-top: 5px !important;">                        
						@Html.DisplayFor(model => model.PrepayAmount)
                    </div>
                </div>
                <div class="control-group">
                    <div class="control-label">
						@Html.DisplayNameFor(model => model.RedemptionPercent)
                    </div>
					<div class="controls-detail" style="margin-top: 5px !important;">
						@Html.DisplayFor(model => model.RedemptionPercent)
                    </div>
                </div>
                }
                    <div class="control-group">
                        <div class="control-label">
                            @Html.DisplayNameFor(model => model.DateStartCount)
                        </div>
                        <div class="controls-detail" style="margin-top: 5px !important;">
                            @Html.DisplayFor(model => model.DateStartCount)
                        </div>
                    </div>

                    <div class="control-group">
                        <div class="control-label">
                            วงเงิน
                        </div>
                        <div class="controls-detail" style="margin-top: 5px !important;">
                            @ViewBag.RequestAmountStr บาท
                        </div>
                    </div>
                    
                    
                    <div class="control-group">
                    <div class="control-label">
						@Html.DisplayNameFor(model => model.RecordStatus)                        
                    </div>
					<div class="controls-detail" style="margin-top: 5px !important;">
                        <span class="@switch (Model.RecordStatus)
                                            {
                                                case "U": @:text-info
                                                                                                                break; case "A": @: text-success
                                                                                                                break; case "W": @: text-warning
                                                                                                                break; case "P": @: muted
                                                                                                                break; case "J": @: red
                                                                                                                break; default: break; }
                              ">
                            @*KPIS.GERP.Models.SYS.RecordStatus.RecordStatusName(Model.RecordStatus)*@
                            @{
                                var db = new GERPContext(true);
                                var task2 = db.Tasks
                                    .Include(o => o.CurrentTaskHistory)
                                    .FirstOrDefault(o => o.RefId == Model.PoId &&
                                                         new int[2] {10, 13}.Contains(o.ApproveProcess.AppPrcGrpId)
                                                         && o.RecordStatus != RecordStatus.DeletedStatus);
                               if (task2 != null)
                                    {
                                        @task2.StatusText
                                    }
                                    else
                                    {
                                        @KPIS.GERP.Models.SYS.RecordStatus.RecordStatusName(Model.RecordStatus)

                                                         
                                                         if (task2 != null && !task2.Completed &&
                                    task2.CurrentTaskHistory != null &&
                                    task2.CurrentTaskHistory.ApproverPosId != null)
                                {
                                    var empOfPos = db.Employees
                                        .Include(o => o.Position)
                                        .FirstOrDefault(o => o.RecordStatus != "D"
                                                             && o.PosId == task2.CurrentTaskHistory.ApproverPosId);


                                    if (empOfPos != null)
                                    {
                                        @: โดย @empOfPos.FullName (@empOfPos.Position.PosNo - @empOfPos.Position.Name) เมื่อ @task2.CurrentTaskHistory.CreatedWhen.Value.ToString("dd MMMM yyyy HH:mm")
                                    }
                                }
                                else if (task2 != null && task2.Completed &&
                                         !string.IsNullOrWhiteSpace(task2.CompletedByFullName))
                                {
                                    @:โดย @task2.CompletedByPosName - @task2.CompletedByFullName เมื่อ @task2.CompletedWhen.Value.ToString("dd MMMM yyyy HH:mm")
                                }
                            }

                                       if (task2 != null && Model.RecordStatus != RecordStatus.ReturnedStatus)
                                        {
                                            @:&nbsp;&nbsp;&nbsp;&nbsp;
                                            <a href="@Url.Action("Details", "Task", new {id=task2.TaskId})" target="_blank">
                                                ไปที่หน้าคำขอ <i class="icon-external-link"></i>
                                            </a>
                                        }
                                        else if (Model.RecordStatus == RecordStatus.ReturnedStatus)
                                        {
                                            @:&nbsp;&nbsp;&nbsp;&nbsp;
                                            <a href="@Url.Action("Details", "Task", new {id=task2.TaskId})" target="_blank" style="background-color: rgb(255, 255, 180); 
                                                                                                                                                                                                                                  font-size:14px;
                                                                                                                                                                                                                                  font-weight: bold;                                                                                                                                  ">
                                                ส่งกลับไปขออนุมัติต่อ (ต้องเป็นผู้สร้างคำขอ)<i class="icon-external-link"></i>
                                            </a>
                                        }

                            }

                        </span>&nbsp;
</div>
                </div>

                </div>
                <div class="span6">
                </div>
                <div class="clearfix"></div>
            </div>
        </div>
    </div>
</div>
}